// Vue过滤器
const getTimeFormat = function (time) {
  //time 1398250549490
  //时间数字转2014-04-23 18:55:49 格式
  // php 后端时间戳返回10位数，补3个 000
  if (time == "0" || time == "0000" || !time) {
    return "----";
  }
  time = time + "";
  var date = "";
  if (time.length >= 13) {
    date = new Date(parseInt(time));
  } else {
    date = new Date(parseInt(time + "000"));
  }
  var Y = date.getFullYear() + "-";
  var M = (date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1) + "-";
  var D = (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + " ";
  var h = (date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) + ":";
  var m = (date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()) + ":";
  var s = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
  return Y + M + D + h + m + s;
};
const getTimeFormat2 = function (time) {
  //time 1398250549490
  //时间数字转2014-04-23 18:55:49 格式
  // php 后端时间戳返回10位数，补3个 000
  if (time == "0" || time == "0000") {
    return "";
  }
  time = time + "";
  var date = "";
  if (time.length >= 13) {
    date = new Date(parseInt(time));
  } else {
    date = new Date(parseInt(time + "000"));
  }
  var Y = date.getFullYear() + "-";
  var M = (date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1) + "-";
  var D = (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + " ";
  return Y + M + D;
};

const toChinesNum = (num) => {
  let changeNum = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"]; //changeNum[0] = "零"
  let unit = ["", "十", "百", "千", "万"];
  num = parseInt(num);
  let getWan = (temp) => {
    let strArr = temp.toString().split("").reverse();
    let newNum = "";
    for (var i = 0; i < strArr.length; i++) {
      newNum = (i == 0 && strArr[i] == 0 ? "" : i > 0 && strArr[i] == 0 && strArr[i - 1] == 0 ? "" : changeNum[strArr[i]] + (strArr[i] == 0 ? unit[0] : unit[i])) + newNum;
    }
    return newNum;
  };
  let overWan = Math.floor(num / 10000);
  let noWan = num % 10000;
  if (noWan.toString().length < 4) noWan = "0" + noWan;
  return overWan ? getWan(overWan) + "万" + getWan(noWan) : getWan(num);
};
const keepTwoNum = function (num, decimal) {
  num = num + "";
  var str = "";
  if (num.indexOf("+") >= 0) {
    str = "+";
  } else if (num.indexOf("-") >= 0) {
    str = "";
  } else {
    str = "";
  }
  num = num.toString();
  // decimal= decimal || 2;
  decimal = decimal == "0" ? 0 : decimal || 2;
  let index = num.indexOf(".");
  if (index !== -1) {
    num = num.substring(0, decimal + index + 1);
  } else {
    num = num.substring(0);
  }
  return str + parseFloat(num).toFixed(decimal);
};
const getMonthAndDay = function (time) {
  time = time + "";
  var date = "";
  if (time.length >= 13) {
    date = new Date(parseInt(time));
  } else {
    date = new Date(parseInt(time + "000"));
  }
  var M = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
  var D = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
  return M + " 月 " + D + " 日";
};
const toFixedNum = function (num) {
  return Number(num).toFixed(2);
};
const toNumber = function (str) {
  return Number(str);
};

const ellipsis = function (value) {
  if (!value) return "";
  if (value.length > 5) {
    return value.slice(0, 5) + "...";
  }
  return value;
};

const getTimeFormat3 = function (time) {
  if (time == "0" || time == "0000") {
    return "";
  }
  time = time + "";
  var date = "";
  if (time.length >= 13) {
    date = new Date(parseInt(time));
  } else {
    date = new Date(parseInt(time + "000"));
  }
  var Y = date.getFullYear() + ".";
  var M = (date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1) + "";
  return Y + M;
};

const getTimeFormat4 = function (time) {
  if (time == "0" || time == "0000") {
    return "";
  }
  time = time + "";
  var date = "";
  if (time.length >= 13) {
    date = new Date(parseInt(time));
  } else {
    date = new Date(parseInt(time + "000"));
  }
  var D = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
  return D;
};

const getTimeFormat5 = function (time) {
  //time 1398250549490
  //时间数字转2014-04-23 18:55:49 格式
  // php 后端时间戳返回10位数，补3个 000
  if (time == "0" || time == "0000") {
    return "----";
  }
  time = time + "";
  var date = "";
  if (time.length >= 13) {
    date = new Date(parseInt(time));
  } else {
    date = new Date(parseInt(time + "000"));
  }
  var h = (date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) + ":";
  var m = (date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()) + ":";
  var s = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
  return h + m + s;
};
export default {
  getTimeFormat,
  getTimeFormat2,
  toChinesNum,
  keepTwoNum,
  getMonthAndDay,
  toFixedNum,
  toNumber,
  ellipsis,
  getTimeFormat3,
  getTimeFormat4,
  getTimeFormat5,
};
